<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>3. 至少使用2种方式实现以下data数据的深拷贝</title>
</head>

<body>
  <script>
    const data = [{
      id: 1,
      name: 'Huawei Mate50 pro',
      price: 6998
    }, {
      id: 2,
      name: 'Apple 18',
      price: 12998
    }, {
      id: 3,
      name: 'xiaomi 12',
      price: 3998
    }]

    // 1.深拷贝 ：JSON.parse(JSON.stringify())；
    const newData = JSON.parse(JSON.stringify(data))
    console.log(newData);

    // 2.深拷贝 ：var newObj3 = Object.assign({},obj);
    const newArr2 = []
    data.map(item=>{
      newArr2.push(Object.assign({},item))
    })
    
    // console.log('data[1]',data[1]);
    data[1].name= 'nn',

    console.log('改变后原数组',data);
    console.log('深拷贝',newArr2);
     
  

  </script>


</body>

</html>